<?php
    namespace app\index\controller;

    use app\index\model\User as UserModel;
    class User
    {
        // 获取用户数据列表
        public function index()
        {
            /* $list = UserModel::scope(
                function($query){
                    $query->where('email','thinkphp@qq.com')
                          ->where('status',0)
                          ->order('id','desc');
                }
            )->select(); */
            $list = UserModel::scope('email','zhangsan@163.com')->select();
            foreach($list as $user)
            {
                echo $user->id ."\t";
                echo $user->nickname .'<br>';
                echo $user->email . '<br>';
                echo $user->birthday.'<br>';
                echo $user->status.'<br>';
                echo $user->create_time.'<br>';
                echo $user->update_time.'<br>';
                echo '---------------------------<br>';
            }     
        }
        // 新增用户数据
        public function add()
        {
            $user = new UserModel();
            $user->nickname = '流年';
            $user->email = 'thinkphp@qq.com';
            $user->birthday = '1977-03-05';
            if($user->save()){
                return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功';
            }else{
                return $user->getError();
            }
        }

        // 简化新增用户数据方式
        public function add2(){
            $data = array(
                'nickname' => '看云',
                'email' => 'kancloud@qq.com',
                'birthday' => '2015-04-02'
            );
            if ($result = UserModel::create($data))
            {
                return '用户[ ' . $result->nickname . ':' . $result->id . ' ]新增成功';    
            }else{
                return '新增失败';
            }
        }

        // 批量新增
        public function addList()
        {
            $userObj = new UserModel();
            $list = [
                ['nickname'=>'张三','email'=>'zhangsan@163.com','birthday'=>'1988-01-15'],
                ['nickname'=>'李四','email'=>'lisi@163.com','birthday'=>'1989-05-30'],
                ['nickname'=>'流年','email'=>'liunian@163.com','birthday'=>'2001-05-30'],
            ];
            if ($userObj->saveAll($list))
            {
                return '批量新增成功';
            } else {
                return $userObj->getError();
            }
        }

        // 根据email读取用户数据
        public function read($id)
        {
            $user = UserModel::get(['id'=>$id]);
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo $user->birthday . '<br/>';
            echo $user->status . '<br/>';
            echo $user->create_time . '<br/>';
            echo $user->update_time . '<br/>';
        }

        // 更复杂的查询则可以使用闭包并配合查询构建器来完成
        public function read2()
        {
            $user = UserModel::get(function($query){
                $query->where('nickname','流年')->where('id','>',5)->order('id','desc');
            });
            echo $user->id.'<br>';
            echo $user->nickname .'<br>';
            echo $user->email . '<br>';
            echo $user->birthday . '<br>';
            echo $user->user_birthday;
        }

        // 更新数据
        public function update($id)
        {
            $user = UserModel::get($id);
            $user->nickname = '刘晨';
            $user->email = 'liuchen@163.com';
            $user->birthday = strtotime('1989-08-06');
            $user->save();
            return '更新数据成功';
        }

        // 删除数据
        public function delete($id)
        {
            $result = UserModel::destroy($id);
            if($result)
            {
                return '删除成功';
            }else{
                return '删除用户不存在';
            }
        }

    }
?>